package com.lf.cloud.fm.business;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.lf.cloud.comm.dao.BlogContentDao;
import com.lf.cloud.comm.dao.BlogTagsDao;
import com.lf.cloud.comm.dao.EnumsDao;
import com.lf.cloud.comm.dao.GrabAimeishishangInfDao;
import com.lf.cloud.comm.modal.dto.TagsDto;
import com.lf.cloud.comm.modal.pojo.BlogContent;
import com.lf.cloud.comm.modal.pojo.BlogTags;
import com.lf.cloud.comm.modal.pojo.Enums;
import com.lf.cloud.comm.modal.pojo.GrabAimeishishangInf;
import com.lf.cloud.comm.webutils.BigNumber;
import com.lf.cloud.comm.webutils.CommMethod;
import com.lf.cloud.comm.webutils.DateUtils;
import com.lf.cloud.comm.webutils.GetWaterId;

public class BlogContentBusiness {
	
	
	public static BlogContentBusiness bus = new BlogContentBusiness();
	
	
	public String inintData(){
		
		
		
		/*
		  
		 	<div class='file-list'>
				<p style='text-align:center'>
					<a href=  target='_blank' title='图片' data-gallery='>
						<img src= ></a>
				</p>
			</div>
		
		 */
		
		
		
		
		
		
		List<Record> inintdata = GrabAimeishishangInfDao.me.inintdata();
		
		
		String geetEnumsVal = EnumsDao.me.geetEnumsVal("_local_ip");
		String html = "";
		if(inintdata!=null && inintdata.size()>0){
			
			List<BlogContent> listObj = null;
			
			for (int i = 0 ; i < inintdata.size() ; i++) {
				
				if(listObj == null){
					listObj = new ArrayList<BlogContent>();
				}
				
				Record record = inintdata.get(i);
				String titleId = record.get("title_id");
				String titleName = record.get("title_name");
				List<GrabAimeishishangInf> list = GrabAimeishishangInfDao.me.geetGrabAimeishishangInfByTitleId(titleId);
				html = "<div class='file-list'>";
				
				//for (GrabAimeishishangInf obj : list) {
				
				String reqUrl = "";
				for (int j = 0; j < list.size(); j++) {
					
					GrabAimeishishangInf obj = list.get(j);
				
					String fileName = obj.getFileName();
					String uploadPath = obj.getUploadPath();
					String imgUrl = "http://"+geetEnumsVal+File.separator+"upload"+File.separator+uploadPath+File.separator+fileName;
					imgUrl = CommMethod.formatPath(imgUrl);
					
					if(j < 3){
						if(j!=0){
							reqUrl += "@";
						}
						reqUrl += imgUrl;
					}
					
					html += "<p style='text-align:center'>";
					html += "<a href='"+imgUrl+"'  target='_blank' title='"+fileName+"' data-gallery=''><img src='"+imgUrl+"' ></a>";
					html += "</p>";
					System.out.println(".");
				}
				html += "</div>";
				
				listObj.add(this.getBlogContentObj(titleName, html , reqUrl));
				
				
				if(listObj.size()==50){
					BlogContentDao.me.insertBatchBlogContent(listObj, 50);
					listObj = null;
					System.out.println("第"+((i+1)/50)+"组已完成, 开始进行下一组数据....");
				}
			}
		}
		System.out.println(inintdata.size());
		return "OK";
	}
	
	
	
	
	public String initTags(){
		String tagsName = "棒糖妹@";
		String[] split = tagsName.split("@");
		for (int i = 0; i < split.length; i++) {
			String selectName = split[i];
			String sql = "select title_id , title_name FROM lexy_grab_aimeishishang_inf WHERE title_name like '%棒糖妹%' GROUP BY title_id";
			List<Record> find = Db.find(sql);
			if(find.size()>0){
				String parentTagNo = GetWaterId.getSerialId();
				List<BlogTags> tagObjList = new ArrayList<BlogTags>();
				for (Record record : find) {
					String tagNo = record.getStr("title_id");
					String tagName = record.getStr("title_name");
					tagObjList.add(getBlogTags(tagNo, tagName, parentTagNo));
				}
				tagObjList.add(getBlogTags(parentTagNo, selectName, ""));
				BlogTagsDao.me.insertBatchBlogTag(tagObjList, tagObjList.size());
			}
		}
		
		return "";
	}
	
	
	
	public BlogTags getBlogTags(String tagNo , String tagName ,String parentTagNo){
		
		BlogTags tagObj = new BlogTags();
		tagObj.setSerialId(GetWaterId.getWaterId());
		tagObj.setTagNo(tagNo);
		tagObj.setTagName(tagName);
		tagObj.setParentTagNo(parentTagNo);
		tagObj.setUpdateTime(DateUtils.getCurrDateTime());
		tagObj.setIsDelete("0");
		return tagObj;
	}
	
	
	public List<TagsDto> getTagsList(){
		List<TagsDto> tagList = new ArrayList<TagsDto>();
		
		List<BlogTags> blogTagsList = BlogTagsDao.me.geetBlogTagNames();
		TagsDto tagsDto= null;
		for (int i = 0; i < blogTagsList.size(); i++) {
			tagsDto = new TagsDto();
			BlogTags blogTags = blogTagsList.get(i);
			String tagName = blogTags.getTagName();
			//List<GrabAimeishishangInf> obj_list = GrabAimeishishangInfDao.me.geetGrabAimeishishangInf_GroupBy(tagName);
			
			System.out.println(tagName);
			
			List<BlogContent> obj_list = BlogContentDao.me.geetBlogContent_GroupBy(tagName);
			
			if(obj_list.size()>0){
				tagsDto.setTagNo(blogTags.getTagNo());
				tagsDto.setTagName(tagName);
				tagsDto.setTagNameCount(String.valueOf(obj_list.size()));
				tagList.add(tagsDto);
			}
		}
		return tagList;
	}
	
	
	
	
	
	/**  
	* @Title: getBlogContentObj  
	* @Description: 拼装Obj
	* @param @param title
	* @param @param content
	* @param @return
	* @return BlogContent
	* @author lxliufei
	* @date 下午6:35:03
	* @throws  
	*/  
	public BlogContent getBlogContentObj(String title , String content , String reqUrl){
		BlogContent bc = new BlogContent();
		bc.setSerialId(GetWaterId.getWaterId());
		bc.setBlogTitle(title);
		bc.setBlogContent(content);
		bc.setReqUrl(reqUrl);
		bc.setClickNum("0");
		bc.setShowType("0");
		bc.setUpdateUserNo("999999");
		bc.setUpdateUserName("admin");
		bc.setUpdateTime(DateUtils.getCurrDateTime());
		bc.setIsDelete("0");
		return bc;
	} 
	
	
	 /**  
	* @Title: getBlogContentPage  
	* @Description: TODO(这里用一句话描述这个方法的作用)  
	* @param pageNumber
	* @param pageSize
	* @param tagNo
	* @return      
	* @returnType Page<BlogContent>      
	* @author LiuFei
	* @date 下午9:51:33
	* @throws  
	*/  
	    
	public Page<BlogContent> getBlogContentPage(int pageNumber , int pageSize , String tagName){
		List<BlogContent> reslist  = new ArrayList<BlogContent>();
		Page<BlogContent> list = BlogContentDao.me.geetBlogContentPage(pageNumber, pageSize, tagName);
		List<BlogContent> objlist = list.getList();
		if(objlist.size()>0){
			for (BlogContent obj : objlist) {
				obj.setBlogContent(null);
				String blogTitle = obj.getBlogTitle();
				if(blogTitle.length()>=20){
					obj.setBlogTitle(blogTitle.substring(0, 20));
				}
				
				
				String reqUrl = obj.getReqUrl();
				if(reqUrl.contains("@")){
					obj.setBlogContent(reqUrl.split("@")[0]);
				}
				reslist.add(obj);
			}
		}
		return new Page<BlogContent>(reslist, list.getPageNumber(), list.getPageSize(), list.getTotalPage(), list.getTotalRow());
	}
	
	  
	/**  
	* @Title: getBlogContentPageHtml  
	* @Description: 获取瀑布流Html代码
	* @param @param pageNumber
	* @param @param pageSize
	* @param @param tagNo
	* @param @return
	* @return String
	* @author lxliufei
	* @date 下午3:46:31
	* @throws  
	*/  
	    
	public String getBlogContentPageHtml(int pageNumber , int pageSize , String tagNo){
		
		Page<BlogContent> list = BlogContentDao.me.geetBlogContentPage(pageNumber, pageSize, tagNo);

		List<BlogContent> ojblist = list.getList();
		String recHtml = "";
		if(ojblist.size()>0){
			
			for (BlogContent obj : ojblist) {
				recHtml += "<li class='item' ><a href='getGalleryContent?serialId="+obj.getSerialId()+"' target='_blank' class='a-img'>"
						+ "<img src='"+obj.getReqUrl().split("@")[0]+"'></a><h2 class='li-title' title='"+obj.getBlogTitle()+"'>"+obj.getBlogTitle()+"</h2>"
								+ "<div class='qianm clearfloat'><span class='sp1'><b>"+obj.getClickNum()+"</b>浏览</span> <span class='sp2'>"+obj.getUpdateUserName()+"</span>"
										+ " <span class='sp3'>"+obj.getUpdateTime()+" &nbsp;By</span></div></li>";
			}
		}
		return recHtml;
	}
	
	
	
	
	public BlogContent getGalleryContent(String serialId){
		
		BlogContent obj = BlogContentDao.me.geetBlogContentById(serialId);
		if(obj!=null){
			BlogContent updateObj = obj;
			String clickNum = updateObj.getClickNum();
			updateObj.setClickNum(BigNumber.add(clickNum, "1"));
			BlogContentDao.me.updateBlogContent(updateObj);
			
			String geetEnumsVal = EnumsDao.me.geetEnumsVal("_local_ip");
			String blogContent = obj.getBlogContent();
			obj.setBlogContent(blogContent.replace("127.0.0.1:8080", geetEnumsVal));
			
		}
		return obj;
	}
	
	
	
	public List<Enums> getPageSize(int pageNumber){
		List<Enums> pageSizeList = new ArrayList<Enums>();
		String pageStr = "8@12@24@36";
		String[] split = pageStr.split("@");
		for (int i = 0; i < split.length; i++) {
			Enums obj = new Enums();
			obj.setEkey(String.valueOf(pageNumber));
			obj.setEvalue(split[i]);
			pageSizeList.add(obj);
		}
		return pageSizeList;
	}
	
	

}
